package com.ws.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.bean.Suppliers;
import com.ws.bean.vo.SupplierVo;
import com.ws.service.Acl_UserService;
import com.ws.service.RouteService;
import com.ws.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
     SupplierService SupplierService;
    @Autowired
    private Acl_UserService acl_UserService;
    @Autowired
    private RouteService routeService;

    // 查询供应商
    @RequestMapping("list")
    public String list(Model model , SupplierVo supplierVo) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(supplierVo.getCurrentPage(),supplierVo.getPageSize());
        List<Suppliers> list = SupplierService.list(supplierVo);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("supplierVo",supplierVo);
        return "supplier/list";
    }

    // 跳转到添加供应商页面
    @RequestMapping("toAdd")
    public String toAdd() {
        return "supplier/add";
    }
    // 添加供应商
    @RequestMapping("add")
    public String add(Suppliers suppliers) {
        SupplierService.add(suppliers);
        return "redirect:list";
    }
    // 删除供应商
    @RequestMapping("delete")
    public String delete(Integer id) {

        int aclUser = acl_UserService.selectUserBySupplierId(id);
        int route = routeService.selectRouteBySupplierId(id);
        if (aclUser>0||route>0){
            return "redirect:list";
        }else {
            SupplierService.delete(id);
        }
        return "redirect:list";
    }
    // 跳转到修改供应商页面
    @RequestMapping("toupdate")
    public String toUpdate(Integer id,Model model){
        Suppliers supplier = SupplierService.getSupplierById(id);
        model.addAttribute("supplier",supplier);
        return "supplier/update";
    }
    @RequestMapping("update")
    public String update(Suppliers suppliers){
        SupplierService.update(suppliers);
        return "redirect:list";
    }
}
